package com.quanyan.user.service.impl;

import com.quanyan.user.cache.MyRedisTemplate;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.mapper.UserInfoMapper;
import com.quanyan.user.model.db.TbLevel;
import com.quanyan.user.model.db.TbLevelParam;
import com.quanyan.user.model.vo.request.ReqUserLevel;
import com.quanyan.user.model.vo.response.RespUserLevel;
import com.quanyan.user.repository.UserLevelRepository;
import com.quanyan.user.service.LevelService;
import com.quanyan.user.service.UserInformationService;
import com.quanyan.user.service.UserLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by wmb on 2016/7/12.
 */
@Service("userLevelService")
public class UserLevelServiceImpl implements UserLevelService {
    @Autowired
    private UserLevelRepository userLevelRepository;
    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private LevelService levelService;
    @Override
    @Valid
    public RespUserLevel modifyExperience(ReqUserLevel reqUserLevel) {
        int modify = 0;
        int uid = reqUserLevel.getUid();
        int experience = reqUserLevel.getExperience();
        //初始化账号
        TbLevel level = userLevelRepository.initAccount(uid, (byte) 1);
        if (level != null) {
            TbLevel newLevel = new TbLevel();
            newLevel.setCommonId(uid);
            int xp = level.getXp();
            int addXp = xp + experience;// 升级之后的经验
            if(level.getNextLevelXp() > addXp ){
                newLevel.setXp(experience);
                newLevel.setLevel(level.getLevel());
                newLevel.setNextLevelXp(level.getNextLevelXp());
            } else {//升级
                int addLevel = level.getLevel() + 2 ;
                List<TbLevelParam> levelParamList = userLevelRepository.getLevelParamList(1,addLevel);
                if (!levelParamList.isEmpty()) {
                    TbLevelParam levelParam = levelParamList.get(0);
                    newLevel.setLevel(level.getLevel()+1);
                    newLevel.setNextLevelXp(levelParam.getXp());
                    newLevel.setXp(experience);
                } else {
                    newLevel.setLevel(level.getLevel());
                    newLevel.setNextLevelXp(level.getNextLevelXp());
                    newLevel.setXp(0);
                }

                TbLevelParam tbLevelParam = levelService.getHighLevel();//最高等级
                if (tbLevelParam != null){
                    if(addLevel > tbLevelParam.getLevel()){
                        newLevel.setLevel(tbLevelParam.getLevel());
                        newLevel.setXp(addXp);
                    }
                }

            }
            userLevelRepository.updateUserLevel(newLevel);
            myRedisTemplate.set(UserConstants.getLevelRedisKey(uid), levelService.getTbLevelDb(uid), 7);
            myRedisTemplate.set(UserConstants.getUserDetailRedisKey(uid),userInfoMapper.findUserInfoDetail(uid),7);
        }
        RespUserLevel respUserLevel =new RespUserLevel();
        respUserLevel.setExperience(experience);
        respUserLevel.setAccount(userLevelRepository.selectByUid(uid));
        //更新用户等级时需要更新用户信息缓存
        if (modify == 1) {
            userInformationService.deleteUserCache(uid);
        }
        return respUserLevel;
    }


}
